/**
 * config.columns=[]; config.columns.name 代表要取的字段名称
 * 
 */
(function($) {
	var isFirst = true;
	var tableNode = null;
	var columns = null;
	var url = null;
	var defaultPageSize = 5;
	var pageSize = defaultPageSize;
	var params = null;
	var isPage = true;
	var currPageNo = 1;
	function createTableBody(data) {
		$('tr:not(:first)', tableNode).remove();
		var header = $('tr:first', tableNode);
		var list = data.list;
		window.gridcache=list;
		if (list && list.length > 0) {
			var html = "";
			for ( var i = 0; i < list.length; i++) {
				html += "<tr><td height='42' align='center'>"+(i+1)+"</td>";
				var row = list[i];
				for ( var j = 0; j < columns.length; j++) {
					if(columns[j].renderer!=null){
//						console.log(columns[j].renderer.call(row,row[columns[j].name]));
							html +="<td align='center'  style='"+(columns[j].style==null?'':columns[j].style)+"'>" +  columns[j].renderer.call(row,row[columns[j].name]) + '</td>';
					}
					else{
						html += "<td align='center'  >" + (row[columns[j].name]==null?'':row[columns[j].name]) + '</td>';
					}
				}
			}
			html += "<tr>";
			header.parent().append(html);
		}
	}
	function createPagingBar(data) {
		$('.page').remove();
		var pageBar2 = "<div id='page' class='page'>共有"+data.count+"条信息，当前<strong><input  id='goto'   value='"+data.pageNo+"'>/"+data.pageNum+" </strong>页";
		pageBar2 +="<a  href='javascript:$.page(1,"+data.pageNum+")'>首页</a>  <a href='javascript:$.page("+(data.pageNo-1)+","+data.pageNo+")'>上一页</a> <a href='javascript:$.page("+(data.pageNo+1)+","+data.pageNum+")'>下一页</a> <a href='javascript:$.page("+data.pageNum+","+data.pageNum+")'>尾页</a><a href='javascript:$.page("+data.pageNo+","+data.pageNum+")'>   刷新 </a></div>";
		tableNode.parent().append(pageBar2);
//		$("#goto").bind("blur",function(){
//		$.page(this.value,data.pageNum);
//		});
		$("#goto").bind("keyup",function(event){
			 if (event.keyCode=="13"){
				var re = /^[1-9]+[0-9]*]*$/;        //判断正整数    
				if (!re.test(this.value))
				{
					alert("请输入数字(例如:1)");
					this.value= 1;
					this.focus();
					return false;
				}else {
					$.page(this.value,data.pageNum);
				}
			}
		});
	}


	$.extend({page:function(pageNo,totalNum) {
		pageNo = parseInt(pageNo);
		if(typeof(pageNo)!='number') {
			alert("请输入正确的页码");
			return;
		}else{
			if(pageNo<=1) {
				pageNo = 1;
			}
			if(pageNo>=totalNum) {
				pageNo = totalNum;
			}
			params.pageNo=pageNo;
			$.post(url, params, function(data) {
				createTableBody(data);
				createPagingBar(data);
				currPageNo = pageNo;
			});
		}
	}
	});

//	鼠标滑入table
	$.extend({fmove:function(event){   
//		var tr=event.target.parentNode;  
//		var table = tr.parentNode;
//		if(table.children[0] != tr) {
//		var array = tr.children; 
//		for ( var i = 0; i < array.length; i++) {
//		var td = array[i];
//		td.style.backgroundColor="#fef7f0";
//		}
//		}
	}
	}); 
//	鼠标滑出table
	$.extend({fout:function(event){   
//		var tr=event.target.parentNode;  
//		var table = tr.parentNode;
//		if(table.children[0] != tr) {
//		var array = tr.children; 
//		for ( var i = 0; i < array.length; i++) {
//		var td = array[i];
//		td.style.backgroundColor="white";
//		}
//		}
	}

	});  



	$.fn.table = function(config,ispage) {
		if(isFirst) {
			var html = "<table class='jquery_table' width='872' border='0'  cellspacing='0' cellpadding='0' onmousemove='$.fmove(event)'   onmouseout='$.fout(event)'><tr>";
			html += "<th class='bk' width='54' height='26' scope='col'>序号 </th>";
			columns = config.columns;
			for ( var i = 0; i < columns.length; i++) {
				html += "<th align='center'  height='26'>" + columns[i].headerText + '</th>';
			}
			html += "</tr>";	
			html += '</table>';
			tableNode = $(html);

			isFirst = false;
		}

		$(this).append(tableNode);
		url = config.url;
		pageSize = config.pageSize;
		params = config.params;
		params.pageNo=params.pageNo&&params.pageNo>0?params.pageNo:currPageNo;
		params.pageSize = params.pageSize || defaultPageSize;
		$.post(url, params, function(data) {
			createTableBody(data);
			if(ispage!=false) {
				createPagingBar(data);
			}
		});

	};
})(jQuery);

